<?php
/**
 * Article.php
 *
 * 日志对象模型类.
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Momoger
 * @package 	Momoger_Model
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Model_Abstract */
//require_once 'Abstract.php';

/**
 * 日志对象模型类.
 *
 * @category 	Momoger
 * @package		Momoger_Model
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class Momoger_Model_Article extends Momoger_Model_Abstract {

	/**
	 * 日志内容模型
	 *
	 * @var Momoger_Model_ArticleContent
	 */
	private $_articleContent = null;

	/**
	 * 日志草稿模型
	 *
	 * @var Momoger_Model_ArticleRough
	 */
	private $_articleRough = null;

	/**
	 * 日志和标签关联对象
	 *
	 * @var Momoger_Model_ArticleToTag
	 */
	private $_articleTag = null;

	/**
	 * 日志和分类关联对象
	 *
	 * @var Momoger_Model_ArticleToCategory
	 */
	private $_articleCategory = null;

	/**
	 * 构造函数.
	 *
	 * @param mixed $options 快速设置参数.
	 */
	public function __construct($options = null) {
        parent::__construct($options);
	}

	/**
	 * 获取日志相关信息
	 *
	 * @param int $article_id 日志ID
	 * @param boolean $with_content 是否需要提供日志内容
	 * @param boolean $with_category 是否需要提供日志分类信息
	 * @param boolean $with_tag 是否需要提供日志标签信息
	 * @return array
	 */
	public function getArticle($article_id, $with_content = false, $with_category = false, $with_tag = false) {
		$this->find($article_id);
		$articleID = $this->get('articleID');
		if ($articleID && $with_content) {
			$this->articleContent()->findRow("articleID = $articleID", 'articleVersion DESC');
			$this->set('articleContent', $this->articleContent()->getAll());
		}
		if ($articleID && $with_category) {
			$this->set('articleCategory', $this->articleCategory()->getArticleCategories($articleID));
		}
		if ($articleID && $with_tag) {
			$this->set('articleTag', $this->articleTag()->getArticleTags($articleID));
		}
		return $this->getAll();
	}

	/**
	 * 修改日志和标签关系数据
	 *
	 * @param int $article_id
	 * @param array $tags
	 * @return unknown
	 */
	public function saveArticleTags($article_id, $tags = null) {
		$_tags = (array)$tags;
		if (!count($_tags) || !$article_id) {
			return false;
		}

		$tags = array();
		foreach ($_tags as $tag) {
			if ($tag) {
				$tags[$tag] = $tag;
			}
		}

		if (!count($tags)) {
			return false;
		}

		//modify old article tag
		$oldTags = $this->articleTag()->getArticleTags($article_id);
		if (count($oldTags)) {
			foreach ($oldTags as $tag) {
				if (in_array($tag['tagText'], $tags)) {
					unset($tags[$tag['tagText']]);
				} else {
					//update tags counts
					$this->articleTag()->tag()->reset()->set(array(
						'tagID' => $tag['tagID'],
						'tagCounts' => $tag['tagCounts'] - 1
					))->update();

					//delete article to tag records
					$this->articleTag()->delete(array('articleID = ' . $article_id, 'tagID = ' . $tag['tagID']));
				}
			}
		}

		//add new article tag
		if (count($tags)) {
			foreach ($tags as $tagText) {
				$tag = $this->articleTag()->tag()->reset()->getTagByValue($tagText);
				if (isset($tag['tagID']) && $tag['tagID']) {
					$this->articleTag()->tag()->reset()->set(array(
						'tagID' => $tag['tagID'],
						'tagCounts' => $tag['tagCounts'] + 1,
						'tagUpdated' => time()
					))->update();

					$this->articleTag()->reset()->set(array(
						'articleID' => $article_id,
						'tagID' => $tag['tagID']
					))->insert();
				} else {
					$this->articleTag()->tag()->reset()->set(array(
						'tagCounts' => 1,
						'tagText' => $tagText
					))->insert();

					$tag = $this->articleTag()->tag()->getAll();
					if (isset($tag['tagID']) && $tag['tagID']) {
						$this->articleTag()->reset()->set(array(
							'articleID' => $article_id,
							'tagID' => $tag['tagID']
						))->insert();
					}
				}
			}
		}

		return true;
	}

	/**
	 * 修改日志和分类关系数据
	 *
	 * @param int $article_id 日志ID
	 * @param array $categories 分类ID数组
	 * @return boolean
	 */
	public function saveArticleCategories($article_id, $category_ids = null) {
		$category_ids = (array)$category_ids;
		if (!count($category_ids) || !$article_id) {
			return false;
		}

		$categories = array();
		foreach ($category_ids as $category_id) {
			if ($category_id) {
				$categories[$category_id] = $category_id;
			}
		}

		if (!count($categories)) {
			return false;
		}

		//modify old article category
		$oldCategories = $this->articleCategory()->getArticleCategories($article_id);
		if (count($oldCategories)) {
			foreach ($oldCategories as $category) {
				if (in_array($category['categoryID'], $categories)) {
					unset($categories[$category['categoryID']]);
				} else {
					//update categories counts
					$this->articleCategory()->category()->reset()->set(array(
						'categoryID' => $category['categoryID'],
						'categoryCounts' => $category['categoryCounts'] - 1
					))->update();

					//delete article to category records
					$this->articleCategory()->delete(array('articleID = ' . $article_id, 'categoryID = ' . $category['categoryID']));
				}
			}
		}

		//add new article category
		if (count($categories)) {
			foreach ($categories as $categoryID) {
				$category = $this->articleCategory()->category()->getCategory($categoryID);
				if (isset($category['categoryID']) && $category['categoryID']) {
					$this->articleCategory()->category()->reset()->set(array(
						'categoryID' => $categoryID,
						'categoryCounts' => $category['categoryCounts'] + 1,
						'categoryUpdated' => time()
					))->update();

					$this->articleCategory()->reset()->set(array(
						'articleID' => $article_id,
						'categoryID' => $categoryID
					))->insert();
				}
			}
		}

		return true;
	}

	/**
	 * 获取日志内容模型
	 *
	 * @return Momoger_Model_ArticleContent
	 */
	public function articleContent() {
		if (null === $this->_articleContent) {
			$this->_articleContent = new Momoger_Model_ArticleContent();
		}
		return $this->_articleContent;
	}

	/**
	 * 获取日志和标签关联对象
	 *
	 * @return Momoger_Model_ArticleToTag
	 */
	public function articleTag() {
		if (null === $this->_articleTag) {
			$this->_articleTag = new Momoger_Model_ArticleToTag();
		}
		return $this->_articleTag;
	}

	/**
	 * 获取日志和分类关联对象
	 *
	 * @return Momoger_Model_ArticleToCategory
	 */
	public function articleCategory() {
		if (null === $this->_articleCategory) {
			$this->_articleCategory = new Momoger_Model_ArticleToCategory();
		}
		return $this->_articleCategory;
	}

	/**
	 * 获取日志草稿模型
	 *
	 * @return Momoger_Model_ArticleRough
	 */
	public function articleRough() {
		if (null === $this->_articleRough) {
			$this->_articleRough = new Momoger_Model_ArticleRough();
		}
		return $this->_articleRough;
	}

    /**
     * 获取数据地图对象.
     *
     * @return Momoger_Model_Mapper_Article
     */
    protected function getMapper() {
        if (null === $this->_mapper) {
            $this->setMapper(new Momoger_Model_Mapper_Article());
        }
        return $this->_mapper;
    }
}